package com.lanou.dllo.soccernews.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.bean.greendao.Users;
import com.lanou.dllo.soccernews.database.UsersDBhelper;

import java.util.List;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private Button logininto;
    private EditText telOruser, password;
    private UsersDBhelper helper;
    private String telOruserString, pwdString;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
//        Users users = new Users("huahua", "123456", "13190159838", null, null, null);
//        helper.addUserInfo(users);
        // Toast.makeText(this,helper.)
    }

    private void init() {
        logininto = (Button) findViewById(R.id.logininto);
        logininto.setOnClickListener(this);
        telOruser = (EditText) findViewById(R.id.telOruser);
        password = (EditText) findViewById(R.id.password);
        helper = UsersDBhelper.getInstance();
    }

    @Override
    public void onClick(View v) {

        telOruserString = telOruser.getText().toString().trim();
        pwdString = password.getText().toString();
        //  Log.i("lanou00", telOruserString + "  " + pwdString + "");
        String flag = helper.getLoginInfo(telOruserString);

        // Log.i("lanou00",  flag + "----->>51");
        switch (v.getId()) {
            case R.id.logininto:
                if (telOruserString != null && telOruserString.length() > 0 && pwdString != null && pwdString.length() > 0) {


                    if ((flag != null && flag.length() > 0)) {
                        if (pwdString.equals(flag)
                                ) {
                            Intent intent = new Intent(this, MyHomeActivity.class);
                            startActivity(intent);
                        }
//                         else {
//                            List<Users> tel = helper.getUsesToTel(telOruserString);
//                            if (tel != null && tel.size() > 0) {
//                                if (pwdString.equals(tel.get(0).getPassword())) {
//                                    Intent intent = new Intent(this, MyHomeActivity.class);
//                                    startActivity(intent);
//                                }
//                            }
//
//                        }

                    }
                } else {
                    Toast.makeText(this, "输入的用户名不能为空", Toast.LENGTH_SHORT).show();
                }

                break;


        }
    }
}
